package org.example.demo.service;

import java.lang.Exception;
import java.lang.String;
import java.util.Arrays;
import javax.annotation.PostConstruct;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.example.demo.model.bo.GoodsCreateGoodsInputBO;
import org.example.demo.model.bo.GoodsDeleteGoodsInputBO;
import org.example.demo.model.bo.GoodsGetGoodsInputBO;
import org.example.demo.model.bo.GoodsGoodsExistsInputBO;
import org.example.demo.model.bo.GoodsGoodsInputBO;
import org.example.demo.model.bo.GoodsUpdateGoodsInputBO;
import org.fisco.bcos.sdk.client.Client;
import org.fisco.bcos.sdk.transaction.manager.AssembleTransactionProcessor;
import org.fisco.bcos.sdk.transaction.manager.TransactionProcessorFactory;
import org.fisco.bcos.sdk.transaction.model.dto.CallResponse;
import org.fisco.bcos.sdk.transaction.model.dto.TransactionResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
@NoArgsConstructor
@Data
public class GoodsService {
  public static final String ABI = org.example.demo.utils.IOUtil.readResourceAsString("abi/Goods.abi");

  public static final String BINARY = org.example.demo.utils.IOUtil.readResourceAsString("bin/ecc/Goods.bin");

  public static final String SM_BINARY = org.example.demo.utils.IOUtil.readResourceAsString("bin/sm/Goods.bin");

  @Value("${system.contract.goodsAddress}")
  private String address;

  @Autowired
  private Client client;

  AssembleTransactionProcessor txProcessor;

  @PostConstruct
  public void init() throws Exception {
    this.txProcessor = TransactionProcessorFactory.createAssembleTransactionProcessor(this.client, this.client.getCryptoSuite().getCryptoKeyPair());
  }

  public TransactionResponse updateGoods(GoodsUpdateGoodsInputBO input) throws Exception {
    // 清理商品名称和描述中可能存在的方括号和引号
    if (input.get_name() != null) {
        String cleanedName = input.get_name().replaceAll("[\\[\\]\"']", "").trim();
        input.set_name(cleanedName);
    }
    
    if (input.get_desc() != null) {
        String cleanedDesc = input.get_desc().replaceAll("[\\[\\]\"']", "").trim();
        input.set_desc(cleanedDesc);
    }
    
    return this.txProcessor.sendTransactionAndGetResponse(this.address, ABI, "updateGoods", input.toArgs());
  }

  public CallResponse goods(GoodsGoodsInputBO input) throws Exception {
    return this.txProcessor.sendCall(this.client.getCryptoSuite().getCryptoKeyPair().getAddress(), this.address, ABI, "goods", input.toArgs());
  }

  public CallResponse getGoods(GoodsGetGoodsInputBO input) throws Exception {
    return this.txProcessor.sendCall(this.client.getCryptoSuite().getCryptoKeyPair().getAddress(), this.address, ABI, "getGoods", input.toArgs());
  }

  public CallResponse goodsExists(GoodsGoodsExistsInputBO input) throws Exception {
    return this.txProcessor.sendCall(this.client.getCryptoSuite().getCryptoKeyPair().getAddress(), this.address, ABI, "goodsExists", input.toArgs());
  }

  public TransactionResponse deleteGoods(GoodsDeleteGoodsInputBO input) throws Exception {
    return this.txProcessor.sendTransactionAndGetResponse(this.address, ABI, "deleteGoods", input.toArgs());
  }

  public CallResponse totalGoods() throws Exception {
    return this.txProcessor.sendCall(this.client.getCryptoSuite().getCryptoKeyPair().getAddress(), this.address, ABI, "totalGoods", Arrays.asList());
  }

  public TransactionResponse createGoods(GoodsCreateGoodsInputBO input) throws Exception {
    // 清理商品名称和描述中可能存在的方括号和引号
    if (input.get_name() != null) {
        String cleanedName = input.get_name().replaceAll("[\\[\\]\"']", "").trim();
        input.set_name(cleanedName);
    }
    
    if (input.get_desc() != null) {
        String cleanedDesc = input.get_desc().replaceAll("[\\[\\]\"']", "").trim();
        input.set_desc(cleanedDesc);
    }

    return this.txProcessor.sendTransactionAndGetResponse(this.address, ABI, "createGoods", input.toArgs());
  }

  public CallResponse getAllGoodsIds() throws Exception {
    return this.txProcessor.sendCall(this.client.getCryptoSuite().getCryptoKeyPair().getAddress(), this.address, ABI, "getAllGoodsIds", Arrays.asList());
  }
}
